<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <title>Shorten Link</title>

    <link rel="stylesheet" href="utilitywindow.css" type="text/css" media="screen" title="no title" charset="utf-8">
    <script src="app:/assets/air/AIRAliases.js" type="text/javascript" charset="utf-8"></script>
    <script src="app:/assets/jquery/jquery.js" type="text/javascript" charset="utf-8"></script>
    <script src="app:/assets/skink/skink.js" type="text/javascript" charset="utf-8"></script>
    <script src="app:/assets/skink/skink.debug.js" type="text/javascript" charset="utf-8"></script>
    <script src="app:/assets/skink/skink.sys.js" type="text/javascript" charset="utf-8"></script>
    <script src="app:/assets/skink/skink.shortlink.js" type="text/javascript" charset="utf-8"></script>
    <script src="app:/assets/lib/webtoolkit.trim.js" type="text/javascript" charset="utf-8"></script>

    <script type="text/javascript" charset="utf-8">
        Skink.Prefs = window.opener.Skink.Prefs;
    </script>

    <script type="text/javascript" charset="utf-8">
        $(document).ready(function() {
            var nativeWin = window.htmlLoader.stage.nativeWindow;
            var startHeight = nativeWin.height;

            // get the pref
            var service = Skink.Prefs.get('url-shortener');
            air.trace("service is "+ service);

            if (service == 'shortie') {
                $('#shorten-custom-hidden').css({display: 'block', visibility: 'visible'});
                nativeWin.height = startHeight + $('#shorten-custom-hidden').height();
            }

            // populate the dropdown
            for (method in Skink.Shortlink.services) {
                air.trace(method)

                if (method[0] != '$') {
                    if (method == service) {
                        $('#url-shortener').append('<option value="'+method+'" selected="selected">'+method+'</option>');
                    } else {
                        $('#url-shortener').append('<option value="'+method+'">'+method+'</option>');
                    }
                }
            }

            $('#url-shortener').bind('change', function() {
                air.trace($('#url-shortener').val());
                Skink.Prefs.set('url-shortener', $('#url-shortener').val());
                if ($('#url-shortener').val() != 'shortie') {
                    nativeWin.height = startHeight;
                    $('#shorten-custom-hidden').css({display: 'none', visibility: 'hidden'});
                } else {
                    $('#shorten-custom-hidden').css({display: 'block', visibility: 'visible'});
                    nativeWin.height = startHeight + $('#shorten-custom-hidden').height();
                }
            });

            // Skink.dump("val:"+$('#shorten-original-link').val());
            $('#shorten-original-link').focus();
            $('#shorten-original-link').val('http://');
            // Skink.dump(air.Clipboard.generalClipboard.formats);
            if(air.Clipboard.generalClipboard.hasFormat(air.ClipboardFormats.TEXT_FORMAT)) {
                var cliptext = air.Clipboard.generalClipboard.getData(air.ClipboardFormats.TEXT_FORMAT);
                if (/^https?:\/\//.test(cliptext)) { // if it starts with http://, we assume this is an URL and put it in the form field
                    $('#shorten-original-link').val(cliptext);
                    Skink.Shortlink.services[service](cliptext);
                }
                $('#shorten-original-link').select();
            }

            $('#shortenLink-form').bind('submit', function() {
            var service = Skink.Prefs.get('url-shortener');
                air.trace("service is "+ service);
                var custom = $('#shorten-custom-link').val();
                if (custom != '') {
                    Skink.Shortlink.services[service]($('#shorten-original-link').val(), custom);
                } else {
                    Skink.Shortlink.services[service]($('#shorten-original-link').val());
                }
            });

            // air.trace(air.NativeApplication.nativeApplication.skinkPrefs);
        })
    </script>


</head>

<body id="shortLinkWindow">

<h1>Shorten Link</h1>

<div id="content">
    <form id="shortenLink-form" onsubmit="return false;">
        <div class="formrow">
            <label for="url-shortener">Service</label>
            <select id="url-shortener" name="url-shortener" onChange="">
                <!-- stuff goes here -->
            </select>
        </div>

        <div class="formrow">
            <label for="original-link">Link</label>
            <input type="text" name="original-link" id="shorten-original-link" value="http://" />
        </div>

        <div class="formrow" id="shorten-custom-hidden" style="display: none; visibility: hidden;">
            <label for="custom-link">Custom</label>
            <input type="text" name="custom-link" id="shorten-custom-link" value="" />
        </div>

        <div class="formrow">
            <label for="short-link">Short</label>
            <input type="text" name="short-link" id="shorten-short-link" value="" />
        </div>

        <div class="formrow" style="text-align:right; margin-top:15px">
            <input type="submit" id="prefs-update-button" name="update" value="Shorten &bull;" />
        </div>
        <div id="verification-result" align="center">&nbsp;</div>
    </form>
</div>

</body>
</html>